
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{p4doc}[2003/08/27 v0.1 P4 style customization]
\RequirePackage{fourier}
\RequirePackage[scaled=0.8]{luximono}
\RequirePackage{helvet}
\RequirePackage{color}
\RequirePackage{colortbl}
\usepackage{changepage}
\usepackage{xifthen}
\usepackage[table]{xcolor}
\usepackage{calc}
\usepackage{relsize}
\usepackage{xspace}

\definecolor{bnfgreen}{HTML}{E6FFD4}
\definecolor{codeblue}{HTML}{DEF0FE}
\definecolor{actionorange}{HTML}{FFEFC3}

% This accumulates the BNF for final summary
\newcommand\bnfsummary{}
\newcommand\addtobnf[1]{\renewcommand\bnfsummary{\bnfsummary #1}}


% Section types for this paper

\newcommand{\SECTION}[2]{\section{#1}\label{sec:#2}}
\newcommand{\SUBSECTION}[2]{\subsection{#1}\label{sec:#2}}
\newcommand{\SUBSUBSECTION}[2]{\subsubsection{#1}\label{sec:#2}}

% support for generating PDF files
\usepackage{ifpdf}

\RequirePackage{fancyhdr}
\pagestyle{fancy}
\fancypagestyle{plain}{\fancyhf{}%
   \renewcommand{\headrulewidth}{0pt}%
   \renewcommand{\footrulewidth}{0pt}}

\cfoot{\thepage}

\fancypagestyle{firstpagestyle}{\fancyhf{}%
  \renewcommand\headrulewidth{0pt}%
   \fancyfoot[R]{\copyright\ 2014-2016, The P4 Language Consortium}}

% include titlesec
\RequirePackage[clearempty]{titlesec}
\newcommand{\mytitlerule}{\titlerule[.5pt]}
\newcommand{\mychaptername}{Chapter}
\newcommand{\setupBookStyle}{%
        \titleformat{name=\part}%
                [block]%
                {\thispagestyle{empty}\centering\scshape\huge}%
                {\Roman{part}}%
                {1ex}%
                {}%
                [\vspace*{0.5\textheight}]%
        \titleformat{name=\chapter}%
                [display]%
                {\sffamily\bfseries\huge}%
                {\textnormal{\sffamily\LARGE\mychaptername{} \thechapter}}%
                {0pt}%
                {\vspace*{2.5mm}}%
                [\vspace*{8mm}]%
        \titleformat{name=\chapter,numberless}%
                [display]%
                {\vspace*{-30mm}\sffamily\bfseries\huge}%
                {}%
                {0pt}%
                {}%
                [\vspace*{2mm}]%
}
\@ifundefined{part}{}{\setupBookStyle}
\titleformat{name=\section}
   {\sffamily\bfseries\large}
   {\thesection}
   {1em}
   {}
\titlespacing{\section}{0pt}{*4}{*2}
\titleformat{name=\subsection}
   {\sffamily\bfseries\normalsize}
   {\thesubsection}
   {0.666em}
   {}
\titlespacing{\subsection}{0pt}{*2}{*1}
\titleformat{name=\subsubsection}
   {\sffamily\bfseries\normalsize}
   {\thesubsubsection}
   {0.55em}
   {}
\titleformat{name=\paragraph}
   [runin]
   {\rmfamily\normalsize\bfseries}
   {}
   {opt}
   {}
   [.\ ]
\titleformat{name=\subparagraph}
   [runin]
   {\itshape}
   {}
   {0pt}
   {}
   [.\ ]
\addtocounter{secnumdepth}{1}

% produce nice footers
\RequirePackage[bottom,multiple,stable]{footmisc} % flushmargin,norule removed

% produce nice headings

\renewcommand{\headrulewidth}{0.5pt}

%% set latex/pdflatex specific stuff
\ifpdf
    \RequirePackage[pdftex,
                hyperindex,
                plainpages=false,
                breaklinks,
                colorlinks,
                citecolor=black,
                filecolor=black,
                linkcolor=black,
                pagecolor=black,
                urlcolor=blue]{hyperref}
    \RequirePackage[pdftex]{graphicx}
    \DeclareGraphicsExtensions{.jpg,.pdf}
    \pdfcatalog {
        /PageMode (/UseNone)
    }
    \RequirePackage{thumbpdf}
\else
    \RequirePackage[ps2pdf]{hyperref}
    \RequirePackage{graphicx}
    \DeclareGraphicsExtensions{.eps,.jpg}
\fi

\RequirePackage{listings}

% scala language description
\lstdefinelanguage{BNF}{%
    alsoletter={-},%
    sensitive,%
}[keywords,comments]%


% scala language description
\lstdefinelanguage{P4}{%
    morekeywords={%
      action, algorithm, apply, attributes, bytes,
      calculated_field, control, counter, current, default, direct,
      drop, else, false, field_list, field_list_calculation, fields,
      header, header_type, hit, if, input, instance_count, last, latest,
      layout, length, mask, max_length, metadata, meter, min_width, miss
      output_width, packets, parse_error, parser, parser_exception, payload,
      primitive_action, register, result, return, saturating, select,
      signed, static, switch, true, type, update, valid, verify, width},
    sensitive,%
    backgroundcolor=\color{codeblue},%
%    morecomment=[l]//,%
%    morecomment=[s]{/*}{*/},%
}[keywords,comments]%

% activate the language and predefine settings
\lstset{%
    basicstyle=\ttfamily,%
    language=P4,%
    aboveskip=3mm,%
    belowskip=3mm,%
    fontadjust=true,%
%    columns=[c]fixed,%
    keepspaces=true,%
%    commentstyle=\itshape,%
    keywordstyle=\bfseries,%
    captionpos=b,%
    framerule=0.3pt,%
    firstnumber=0,%
    numbersep=1.5mm,%
    numberstyle=\tiny,%
}


% Primitive action macros

% Parameter documentation inside of a prim action declaration
\newcommand{\paramdoc}[2]{\texttt{#1} & #2 \\ }

\newcommand{\matchaction}{match\raise.30ex\hbox{\smaller{+}}{\allowbreak}action\xspace}
\newcommand{\Matchaction}{Match\raise.30ex\hbox{\smaller{+}}{\allowbreak}action\xspace}
\newcommand{\MatchAction}{Match\raise.30ex\hbox{\smaller{+}}{\allowbreak}Action\xspace}

% Define a primitive action
%   Macro params are:  name, summary, parameters, description
%   Parameters (for action) is a sequence of \paramdoc decs
\newcommand{\primact}[4]{%
\begin{tabular}{|l|}
\hline
\cellcolor{actionorange}%
\begin{minipage}{.95\textwidth}%
\vspace{2mm}%
\texttt{#1}%
\vspace{2mm}%
\newline%
\textbf{Summary}%
\begin{adjustwidth}{1.5cm}{4mm}%
#2%
\end{adjustwidth}%
\ifthenelse{\isempty{#3}}{}{%
\textbf{Parameters}%
\begin{adjustwidth}{1.3cm}{4mm}%
\begin{tabularx}{\textwidth-1.3cm}{p{3cm}X}%
#3%
\end{tabularx}%
\end{adjustwidth}%
}% if #3 empty
\textbf{Description}%
\begin{adjustwidth}{1.5cm}{4mm}%
#4%
\end{adjustwidth}%
\vspace{2mm}%
\end{minipage}%
\\
\hline
\end{tabular}%
}

\newcommand{\docauthor}{Author}
\newcommand{\doctitle}{Title}
\newcommand{\docsubtitle}{Version 1.0.0}
\newcommand{\docdate}{}
\newcommand{\translatedby}{}

\endinput
